package com.know.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import my.db.QueryHelper;

import org.apache.commons.lang.StringUtils;

import com.know.entity.Timeline;
import com.kzw.bean.Record;
import com.kzw.dao.support.Page;

public class TimelineDao {

	/**
	 * 数据处理 得到年、月、日  月日时间 4项
	 * @return
	 */
	public   List<Timeline> list() {
		String sql="select *from timeline order by timeline_time DESC";
		List<Timeline> list=QueryHelper.query(Timeline.class, sql);
	    /*分割时间*/
	    Calendar ca=Calendar.getInstance();
		for (Timeline timeline : list) {
			ca.setTime(timeline.getTimeline_time());
			 int year=ca.get(Calendar.YEAR);
			 int month=ca.get(Calendar.MONTH)+1;
			 int day=ca.get(Calendar.DATE);
			 int hour=ca.get(Calendar.HOUR);
			 int minute=ca.get(Calendar.MINUTE);
			timeline.setTimeline_day(month+"月"+day+"日 "+hour+":"+minute);
			timeline.setTimeline_year(year+"年");
			timeline.setTimeline_month(month+"月");
		}
		return list;
	}
	/** 
	 * 获取现在时间,这个好用 
	 *  
	 * @return返回长时间格式 yyyy-MM-dd HH:mm:ss 
	 */  
	public static Date getSqlDate() {  
	    Date sqlDate = new java.sql.Date(new Date().getTime());
	    return sqlDate;  
	}  
	public static void main(String[] args) {
		List<Timeline> list=new ArrayList<Timeline>();
		/*三层放置时间*/
		
		List<String> days=new ArrayList<String>();
		Map<String,Object> months=new LinkedHashMap<String, Object>();
		Map<String,Object> years=new LinkedHashMap<String, Object>();
		String month=new String(list.get(0).getTimeline_month());
		String year=new String(list.get(0).getTimeline_year());

	
		for (Timeline timeline : list) {
			
			if(timeline.getTimeline_year().equals(year))
			{
				if(timeline.getTimeline_month().equals(month))
				{
//					System.out.println("月"+timeline.getTimeline_month());
					days.add(timeline.getTimeline_day());
//					System.out.println("timeline.getTimeline_day()"+timeline.getTimeline_day());
				}
				else
				{
					months.put(month, days);
					
//					System.out.println("days:"+days.toString());
//					days.clear();
					days=new ArrayList<String>();
					month=timeline.getTimeline_month();
//					System.out.println("timeline.getTimeline_day()"+timeline.getTimeline_day());
					days.add(timeline.getTimeline_day());
				}
				months.put(month, days);

			}
			else{
				years.put(year, months);
				months=new LinkedHashMap<String, Object>();
				year=timeline.getTimeline_year();
				if(timeline.getTimeline_month().equals(month))
				{
//					System.out.println("月"+timeline.getTimeline_month());
					days.add(timeline.getTimeline_day());
//					System.out.println("timeline.getTimeline_day()"+timeline.getTimeline_day());
				}
				else
				{
					months.put(month, days);
					
//					System.out.println("days:"+days.toString());
//					days.clear();
					days=new ArrayList<String>();
					month=timeline.getTimeline_month();
//					System.out.println("timeline.getTimeline_day()"+timeline.getTimeline_day());
					days.add(timeline.getTimeline_day());
				}
				months.put(month, days);
				
			}
		
		}

		years.put(year, months);
		for (Map.Entry<String, Object> entry : months.entrySet()) {
			
			System.out.println(entry.getKey()+" "+entry.getValue().toString());
		}
	
	
	}
	/**
	 * 时间数据分类
	 * @param list
	 * @return
	 */
	public Map<String, Object> sort_list(List<Timeline> list) {
		
		//List<String> days=new ArrayList<String>();
		List<String> cotents=new ArrayList<String>();
		Map<String,Object> days=new LinkedHashMap<String, Object>();
		Map<String,Object> months=new LinkedHashMap<String, Object>();
		Map<String,Object> years=new LinkedHashMap<String, Object>();
		String month=new String(list.get(0).getTimeline_month());
		String year=new String(list.get(0).getTimeline_year());

	
		for (Timeline timeline : list) {
			
			if(timeline.getTimeline_year().equals(year))
			{
				if(timeline.getTimeline_month().equals(month))
				{
//					System.out.println("月"+timeline.getTimeline_month());
					days.put(timeline.getTimeline_day(),timeline.getTimeline_content());
//					System.out.println("timeline.getTimeline_day()"+timeline.getTimeline_day());
				}
				else
				{
					months.put(month, days);
					
//					System.out.println("days:"+days.toString());
//					days.clear();
					days=new LinkedHashMap<String, Object>();
					month=timeline.getTimeline_month();
//					System.out.println("timeline.getTimeline_day()"+timeline.getTimeline_day());
					days.put(timeline.getTimeline_day(),timeline.getTimeline_content());
					
				}
				months.put(month, days);

			}
			else{
				years.put(year, months);
				months=new LinkedHashMap<String, Object>();
				year=timeline.getTimeline_year();
				if(timeline.getTimeline_month().equals(month))
				{
//					System.out.println("月"+timeline.getTimeline_month());
					days.put(timeline.getTimeline_day(),timeline.getTimeline_content());
						
					//System.out.println("timeline.getTimeline_day()"+timeline.getTimeline_day());
				}
				else
				{
					months.put(month, days);
					
//					System.out.println("days:"+days.toString());
//					days.clear();
					days=new LinkedHashMap<String, Object>();
					month=timeline.getTimeline_month();
//					System.out.println("timeline.getTimeline_day()"+timeline.getTimeline_day());
					days.put(timeline.getTimeline_day(),timeline.getTimeline_content());
											}
				months.put(month, days);
				
			}
		
		}

		years.put(year, months);
		return years;
	}
	/**
	 * 分页查询
	 */
	public Record findPage(Page<Timeline> page) {
		Record record = new Record();
		String sql = "select * from timeline";
		if(StringUtils.isNotBlank(page.getOrderBy())) {
			sql += String.format(" order by %s %s", page.getOrderBy(), page.getOrder());
		}
		
		List<Timeline> list = QueryHelper.queryPage(Timeline.class, sql, page.getPageNo(), page.getPageSize());

	
		//统计总数count
		String sql2 = sql.toLowerCase().replaceFirst("select .*? from", "select count(*) from");
		long count = QueryHelper.stat(sql2);
		
		record.setCount(count);
		record.setData(list);
		
		return record;
	
	}
	/**
	 * 保存时间轴
	 */
	public void save(Timeline timeline) {
		String sql= "insert into timeline values(null,?,?,?)";//时间轴表
		QueryHelper.update(sql,1,timeline.getTimeline_content(),timeline.getTimeline_time());
	
	}
	/**
	 * 修改时间轴
	 */
	public void update(Timeline timeline) {
		String sql="UPDATE timeline SET timeline_content=? WHERE (timeline_id=?)";
		QueryHelper.update(sql,timeline.getTimeline_content(),timeline.getTimeline_id());
	}
	/**
	 * 根据ID查找
	 */
	public Timeline finById(long parseLong) {
		String sql="select *from timeline where timeline_id=?";
		Timeline timeline=QueryHelper.queryUnique(Timeline.class, sql, parseLong);
		return timeline;
	}
	/**
	 * 删除时间轴
	 */
	public Record  del(long id) {
		String sql="delete from timeline where timeline_id =?";
		String msg="成功删除";
		Record record=new Record(true);
	
		
			QueryHelper.update(sql, id);
		record.setMsg(msg);
		return record;
	}
	/**
	 * 批量删除
	 * @param ids
	 */
	public Record multiDel(String ids) {
		String sql = "delete from timeline where timeline_id in (" + ids + ")";
		String msg="成功删除";
		Record record=new Record(true);

			QueryHelper.update(sql);
		
		record.setMsg(msg);
		return record;
		
	}
	  

}
